package com.cy.tools;

import com.cy.bean.User;
import com.cy.bean.Userbasicmessage;
import com.cy.bean.Userdetailmessage;
import com.cy.bean.Userloverrequest;
import com.cy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class Bubbling {
//    private  List<Userbasicmessage> userList;
//    private int uid;

    @Autowired
    private UserService userService;


    public  List<Userbasicmessage> userList(List<Userbasicmessage> userList1,int uid){
        System.out.println(userList1+"冒泡+++"+uid);
        List<Userbasicmessage> userList = new ArrayList<>();
        int integralSum = 0;
        Userdetailmessage userdetail = null;
        Userloverrequest userLove=null;
        userLove=userService.selectUserLove(uid);
        for (Userbasicmessage user1: userList1) {
            userdetail=userService.selectUserDetail((int) user1.getUid());
            integralSum=0;//清空权重分
            if (userLove == null || userdetail == null) {
                integralSum=0;
            }else{

    //            性别要求
                if (user1.getUsex().equals(userLove.getUsexrequest())) {
                    integralSum+=20;
                }
    //            学历要求
                if (user1.getUeducate().equals("高中及高中以下")) {
                    integralSum+=10;
                }else if (user1.getUeducate().equals("大专")) {
                    integralSum+=20;
                }else if (user1.getUeducate().equals("本科")) {
                    integralSum+=30;
                }else if (user1.getUeducate().equals("硕士")) {
                    integralSum+=40;
                }else if (user1.getUeducate().equals("博士")) {
                    integralSum+=50;
                }
    //            收入要求
                int income1 = Integer.parseInt(user1.getIncome());
                int income2 =  Integer.parseInt(userLove.getUincome());
                if (income1 >= income2) {
                    integralSum+=50;
                }
    //            血型要求
                if (userdetail.getUblood().equals(userLove.getUbloodrequest())) {
                    integralSum+=20;
                }
    //            工作要求
                if (userdetail.getUcompany() .equals(userLove.getUcompanyrequest())) {
                    integralSum+=30;
                }
    //            语言要求
                if (userdetail.getUlanguage() .equals(userLove.getUlanguagerequest())) {
                    integralSum+=30;
                }
    //            是否抽烟
                if (userdetail.getUsmoking() .equals(userLove.getUsmokingrequest())) {
                    integralSum+=20;
                }
            }

            System.out.println("权重分："+integralSum);
            user1.setIntegralSum(integralSum);
            userList.add(user1);
        }
        if (userList != null) {
            int i,j;
            int n = userList.size();
//        外部循环n次
            for (i=0;i<n;i++){
//            内部循环理解，三个数需要执行该步骤2次，即n-1次。【1-（n-i)）则是剩余未排序数量n的n-1次循环。
//            当i=0时你就可以显而易见的看出来循环次数
                for (j=1;j<n-i;j++){
//                判断，如果后面数小，则替换
                    if (userList.get(j).getIntegralSum()<userList.get(j-1).getIntegralSum()){
                        Userbasicmessage temp;
                        temp=userList.get(j); //
                        userList.add(j, userList.get(j-1) );
                        userList.add(j-1, temp );
                    }
                }
            }
            return userList1;
        }else {
            return userList1;
        }
    }

}

